package persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import modeloDatos.ClienteTelefono;

public class DAOsms {
	private Connection conn;
	public void finalize(){
        ConnectionManager.getInstance().checkIn(conn);
    }
	public ClienteTelefono consultarCliente(String identificacion){
		ClienteTelefono c = null;
		conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		String sql = "SELECT * FROM clientes WHERE cif = ? OR nif = ?";
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, identificacion);
			stmt.setString(2, identificacion);
			ResultSet result = stmt.executeQuery();
			if (result.next()) {
				String ident = "";
				if(!result.getString("cif").equals("")){
					ident = result.getString("cif");
				}else{
					ident = result.getString("nif");
				}
				c = new ClienteTelefono(result.getString("nombre")+result.getString("apellidos"),ident,result.getInt("telefonoMovil"));
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			finalize();
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return c;
	}
}
